/*
 * Copyright 2018-present KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.henghai.zhenxuan.ui.page.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.config.AppConfig;
import com.henghai.zhenxuan.data.bean.HttpBean;
import com.henghai.zhenxuan.data.bean.LoginBean;
import com.henghai.zhenxuan.domain.request.AccountRequester;
import com.henghai.zhenxuan.utils.Utils;
import com.henghai.zhenxuan.R;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;
import com.kunminx.architecture.utils.ToastUtils;

public class LoginFragment extends BaseFragment {
    private LoginStates mStates;
    private AccountRequester mAccountRequester;


    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(LoginStates.class);
        mAccountRequester = getFragmentScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(com.henghai.zhenxuan.R.layout.fragment_login, BR.vm, mStates)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mAccountRequester.getTokenResult().observe(getViewLifecycleOwner(), dataResult -> {
            if (!dataResult.getResponseStatus().isSuccess()) {
                mStates.loadingVisible.set(false);
                ToastUtils.showLongToast(getString(R.string.network_state_retry));
                return;
            }
            HttpBean<LoginBean> loginDataHttpData = dataResult.getResult();
            if (loginDataHttpData.getData() == null || TextUtils.isEmpty(loginDataHttpData.getData().getToken())) {
                return;
            }
            AppConfig.INSTANCE.getMConfigs().token().set(loginDataHttpData.getData().getToken());
            mStates.loadingVisible.set(false);
            nav().navigateUp();
        });
    }

    public class ClickProxy {

        public void back() {
            nav().navigateUp();
        }

        public void login() {
            if (TextUtils.isEmpty(mStates.name.get()) || TextUtils.isEmpty(mStates.password.get())) {
                ToastUtils.showLongToast(getString(R.string.username_or_pwd_incomplete));
                return;
            }
            String packageName = getActivity().getPackageName();
            String phoneSha1 = Utils.getSha1(getActivity());
            mAccountRequester.requestLogin(getViewLifecycleOwner(), mStates.name.get(), mStates.password.get(),packageName,phoneSha1);
            mStates.loadingVisible.set(true);
        }
    }

    public static class LoginStates extends StateHolder {

        public final State<String> name = new State<>("");
        public final State<String> password = new State<>("");
        public final State<Boolean> loadingVisible = new State<>(false);
    }

}
